/*
 * 常用方法
 * @author li.xiaomei
 * @date 2016-12-19
 */
var path = require('path'),
    ExtractTextPlugin = require('extract-text-webpack-plugin');

//查找命令行参数
var findArgv = function(item){
    var argvs = process.argv;

    for(var i = 0, len = argvs.length; i<len; i++){
        if(argvs[i].indexOf(item)!==-1){
            return argvs[i];
        }
    }

    return '';
}
//找到process.argv里面对应参数
exports.findArgv = findArgv;


//数据类型判断
var class2type = {};
var isArray = Array.isArray || function(object){ return object instanceof Array }
function type(obj) {
    return obj == null ? String(obj) :
      class2type[toString.call(obj)] || "object"
}
function isFunction(value) { return type(value) == "function" }
function isWindow(obj)     { return obj != null && obj == obj.window }
function isDocument(obj)   { return obj != null && obj.nodeType == obj.DOCUMENT_NODE }
function isObject(obj)     { return type(obj) == "object" }
function isPlainObject(obj) {
    return isObject(obj) && !isWindow(obj) && Object.getPrototypeOf(obj) == Object.prototype
}


//对象拷贝
function _extend(target, source, deep){
    for(var key in source){
        if(deep && (isPlainObject(source[key]) || isArray(source[key]))){
            if (isPlainObject(source[key]) && !isPlainObject(target[key])){
              target[key] = {}
            }
            if (isArray(source[key]) && !isArray(target[key])) {
              target[key] = []
            }

            //递归拷贝
            _extend(target[key], source[key], deep);

        } else if(source[key] !== undefined){
            target[key] = source[key]
        }
    }
}
exports.extend = function(target){
    var deep,
        args = [].slice.call(arguments, 1);

    if(typeof target === 'boolean'){
        deep = target;
        target = args.shift();
    }

    args.forEach(function(arg){
        _extend(target, arg, deep)
    });

    return target;
}

//css loader处理
var cssLoaders = function(options){
    options = options || {};

    //组装loader
    function generateLoaders(loaders){
        var sourceLoader = loaders.map(function(loader){
            var extraParamChar
            if(/\?/.test(loader)){
              loader = loader.replace(/\?/, '-loader?');
              extraParamChar = '&'
            }else {
              loader = loader + '-loader'
              extraParamChar = '?'
            }

            return loader + (options.sourceMap ? extraParamChar + 'sourceMap' : '');

      }).join('!');

      //如果是在编译beta环境的代码则不需要ExtractTextPlugin
      if(options.extract && (!findArgv(global.build_beta)) ) {
        return ExtractTextPlugin.extract('vue-style-loader', sourceLoader)
      }else {
        return ['vue-style-loader', sourceLoader].join('!')
      }
    }

    return {
      css: generateLoaders(['css']),
      postcss: generateLoaders(['css']),
      less: generateLoaders(['css', 'less']),
      sass: generateLoaders(['css', 'sass?indentedSyntax']),
      scss: generateLoaders(['css', 'sass']),
      stylus: generateLoaders(['css', 'stylus']),
      styl: generateLoaders(['css', 'stylus'])
    }
}
var styleLoaders = function(options){
    var output = []
    var loaders = cssLoaders(options)

    for(var extension in loaders) {
        var loader = loaders[extension]
        output.push({
            test: new RegExp('\\.' + extension + '$'),
            loader: loader
        });
    }

    return output
}
exports.cssLoaders = cssLoaders
exports.styleLoaders = styleLoaders


exports.assetsPath = function (_path) {
  var webpack_config = global.think_config.webpack
  var assetsSubDirectory = webpack_config.build.assetsSubDirectory;
  return path.posix.join(assetsSubDirectory, _path)
}
